/**
 * Created by Administrator on 2018/4/3.
 */
layui.use(['form'], function () {
    var form = layui.form
        , layer = layui.layer;

    function initPage() {
        var token = sessionStorage.access_token;
        if(token==null){
            againToken();
            return;
        }
        //限高
        $('.my-challenge-tab-content').height(winH - 115 + 'px');

        var vue = new Vue({
            el: '#index_div',
            data: {
                history_answer_money: 0,    //总挑战金
                config_info: {},   //用户配置信息
                index:-1,        //撤回挑战的ID
                type: 0,   //0 挑战记录  1挑战卡明细
                my_challenge_list: {},  //挑战记录
                my_challenge_next_page: 1,//挑战记录 下一页的页码
                my_challenge_allpage: 1,//挑战记录 总页码，会从后台获取
                my_challenge_page_size: 6, //挑战记录 每页条数
                card_log_list: {},      //挑战卡明细
                card_log_next_page: 1,//挑战卡明细 下一页的页码
                card_log_allpage: 1,//挑战卡明细 总页码，会从后台获取
                card_log_page_size: 6, //挑战卡明细 每页条数
                card_log_total_record: 0,    //挑战卡明细 总数据量
            },
            watch: {},
            methods: {
                switchType: function (val) {
                    if (this.type == val) {
                        return;
                    }
                    this.type = val;
                },
                onClosePopupVue: function (e) {
                    onCloseClickPopup(e.currentTarget);
                },
                onBackChallenge:function(index){  //确定撤回挑战
                    $('#back_money_pop_up').show();
                    $('html,body').addClass('ovfHiden'); //使网页可滚动
                    initPopupHeight();
                    this.index = index;
                },
                onConfrimBackChallenge:function(){
                    $('#back_money_pop_up').hide();
                    $('html,body').removeClass('ovfHiden'); //使网页可滚动

                    var newArr = this.my_challenge_list[this.index];
                    newArr.status = 2;
                    newArr.can_be_recall = 0;

                    var data = {
                        token:token,
                        red_envelope_id: newArr.id
                    }
                    $.post("/index/challenge/ajax_withdraw", data, function (result) {
                        console.info(result);
                        //if(result.code==-1){
                        //    layer.msg(result.msg);
                        //}
                        if (result.code == 0) {
                            againToken();
                            return
                        }
                        if(result.code==1){
                            layer.msg(result.msg);  //输出提示信息
                            this.set(this.my_challenge_list,this.index,newArr);

                        }

                    });
                }
            },
            filters: {
                timestampToTime: function (val) {
                    return formatDate(val);
                },
                integral: function (value) {
                    return parseInt(value);
                },
                getAnswerHref : function(val){
                    return 'answer.html?id='+val;
                }

            }
        });

        /**
         * 滚动事件
         */
        function scrollFn() {
            //真实内容的高度
            var pageHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            //视窗的高度
            var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
            //隐藏的高度
            var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
            if (pageHeight - viewportHeight - scrollHeight < 20) {  //如果满足触发条件，执行
                if (vue.type == 0) {
                    if (vue.my_challenge_next_page <= vue.my_challenge_allpage) {
                        initMyChallengeList();
                    }
                }
                if (vue.type == 1) {
                    if (vue.card_log_next_page <= vue.card_log_allpage) {
                        initCardLogList();
                    }
                }
            }
        }

        $('.my-challenge-tab-content').bind("scroll", scrollFn);    //绑定滚动事件

        /**
         * 初始化我的挑战记录
         */
        function initMyChallengeList() {
            var data = {
                token: token,
                page_size: vue.my_challenge_page_size,
                page_num: vue.my_challenge_next_page,
            };
            //获取挑战列表信息
            $.ajax({
                type: "post"
                , url: '/index/challenge/ajax_my_challenge_list'
                , data: data
                , async: false
                , success: function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        againToken();
                        return;
                    }
                    if (result.code == 1) {

                        if (result.data.page.pagenum == "1") {
                            vue.my_challenge_list = result.data.list;
                        } else {
                            vue.my_challenge_list.push.apply(vue.my_challenge_list, result.data.list);
                        }
                        vue.my_challenge_next_page = parseInt(result.data.page.pagenum) + 1;
                        vue.my_challenge_allpage = result.data.page.totalpage;
                    }
                }
                , error: function (resq) {
                    console.info(resq);
                    layer.msg("服务器异常");
                }
            });
        }

        /**
         * 初始化挑战卡明细
         */
        function initCardLogList() {
            var data = {
                token: token,
                page_size: vue.card_log_page_size,
                page_num: vue.card_log_next_page,
            };
            //获取挑战列表信息
            $.ajax({
                type: "post"
                , url: '/index/challenge/ajax_card_log_list'
                , data: data
                , async: false
                , success: function (result) {
                    //console.log(result);
                    if (result.code == 0) {
                        againToken();
                        return;
                    }
                    if (result.code == 1) {

                        if (result.data.page.pagenum == "1") {
                            vue.card_log_list = result.data.list;
                        } else {
                            vue.card_log_list.push.apply(vue.card_log_list, result.data.list);
                        }
                        vue.card_log_next_page = parseInt(result.data.page.pagenum) + 1;
                        vue.card_log_allpage = result.data.page.totalpage;
                        vue.card_log_total_record = result.data.page.totalrecord;
                    }
                }
                , error: function (resq) {
                    console.info(resq);
                    layer.msg("服务器异常");
                }
            });
        }

        /**
         * 初始化类型
         */
        function initType(){
            vue.type =  parseInt(getQueryString('type'))==1?1:0;
        }

        /**
         * 初始化历史答题获得的奖励卡
         */
        function initHistoryAnswerMoney(){
            var data = {
                token:token
            }
            $.post("/index/challenge/ajax_history_card", data, function (result) {
                //console.info(result);
                //if (result.code == -1) {   //失败
                //    layer.msg(result.msg);
                //}
                if (result.code == 0) { //Token失效
                    againToken();
                    return;
                }
                if (result.code == 1) { //成功
                    vue.history_answer_money = result.data.history_answer_money;
                }

            });
        }



        initType();
        initMyChallengeList();
        initHistoryAnswerMoney();
        initCardLogList();
        initConfigInfo(vue, token);

    };

    function initPopupHeight() {
        var bmH = $('.back-money-box').outerHeight();
        $('.back-money-box').css('marginTop', (winH - bmH) * 0.45 + 'px');
    };

    //撤回红包
    $('.withdraw-btn').click(function () {

    });
    $('.back-money-box').click(function (e) {
        e.stopPropagation();
    });


    /**
     * 初始化完成加载函数
     */
    $(function () {
        initGlobalPage();
        initPage();
    })


});


